<?php
/**
 * standard interface
 *
 *     SCMwiki Copyright (C) 2010  Callan Bryant <callan1990@googlemail.com>
 *
 *     Based on Voswork - A simple, fast PHP filesystem abstraction layer
 *     Voswork Copyright (C) 2010  Callan Bryant <callan1990@googlemail.com>
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// load the main interface to core
require_once __DIR__.'/../kernel.class.php';

// bootstrap the kernel to set up the voswork environment
kernel::bootstrap();

try
{
	// node?
	if (@$_REQUEST['node'])
		loadNode($_REQUEST['node']);

	// internal file request?
	elseif (@$_REQUEST['file'])
		new internalFile($_REQUEST['file']);

	// wiki page, index or welcome
	else
	{
		$paths = new manifest(WIKI_FILE_REGEX,WIKI_DIR);

		// specific page requested?
		if (@$_SERVER['QUERY_STRING'])
		{
			$name = urldecode(@$_SERVER['QUERY_STRING']);
			if ($path = $paths->$name)
				echo new wikiPage($path);
			else
				throw new Exception('That page does not exist yet');
		}
		// Root request: load home page?
		elseif($path = $paths->Home)
			echo new wikiPage($path);
		// New, empty wiki?
		elseif ($paths->dump() == array())
			// no pages, new wiki, show welcome
			loadNode('welcome');
		else
			// do a flat listing of all pages
			// note that because Home was not found, the manifest
			// will be up to date
			echo new indexPage($paths->dump());
	}
}
catch (Exception $e)
{
	// cancel any previous output
	while (@ob_end_clean() );

	// in cli mode, html looks awful. Let voswork handle the exception
	if (CLI_MODE)
		throw $e;

	// make a nice looking error page
	$error = $e->getMessage();
	$page = new template;
	$page->title = 'Abandon ship!';
	$page->description = 'an error has occurred';
	$page->article =  "<pre>$error</pre>";
	echo $page;
}

function loadNode($node)
{
	// instantiate a new manifest matching nodes in the system dir
	$nodes = new manifest(NODE_MANIFEST_REGEX,ROOT_DIR);

	// find corresponding path
	$path = $nodes->$node;

	if ($path == null)
		throw new Exception('Node "'.strip_tags($node).'" not found');

	$ext = end(explode('.',$path));

	if ($ext === 'php')
	{
		// file is a dynamic php script, execute it (in the correct directory)
		chdir( dirname($path) );
		require_once $path;
	}
	else
	{
		// file is static, let http class handle it
		$s = new httpResponse();
		$s->load_local_file($path);

		if (PERSISTENT_STATIC_NODES)
			$s->persistent = true;

		// override name (to remove .node)
		$s->name = $node.'.'.$ext;
		$s->serve();
	}
	// no more output is allowed
	die();
}
?>
